﻿using System;
using System.ComponentModel;
using EX_NFC.FrontEnd.Windows.ViewModel;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;

namespace EX_NFC.FrontEnd.Windows
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : INotifyPropertyChanged
    {
        private Exception _exception;
        private Exception Exception
        {
            get { return _exception; }
            set { _exception = value; NotifyPropertyChanged("ExceptionType"); NotifyPropertyChanged("ExceptionMessage"); NotifyPropertyChanged("ExceptionStacktrace"); }
        }

        public string ExceptionType { get { return Exception == null ? string.Empty : Exception.GetType().Name; } }

        public string ExceptionMessage { get { return Exception == null ? string.Empty : Exception.Message; } }

        public string ExceptionStacktrace { get { return Exception == null ? string.Empty : Exception.StackTrace; } }

        private RelayCommand _commandCloseExceptionWindow;
        public RelayCommand CommandCloseExceptionWindow
        {
            get
            {
                return _commandCloseExceptionWindow ?? (_commandCloseExceptionWindow = new RelayCommand(() => ErrorWindow.WindowState = Microsoft.Windows.Controls.WindowState.Closed));
            }
        }


        public MainWindow()
        {
            InitializeComponent();

            ErrorWindow.DataContext = this;

            Closing += (s, e) => ViewModelLocator.Cleanup();

            Messenger.Default.Register<GenericMessage<Exception>>(this, MessageType.ErrorMessage, (ex) => { Exception = ex.Content; ErrorWindow.WindowState = Microsoft.Windows.Controls.WindowState.Open; });

            Messenger.Default.Register<GenericMessage<StatusData>>(this, MessageType.StatusMessage, (statusmessage) => SetStatus(statusmessage.Content));

            DeviceStatusText.Text = string.Format(Properties.Resources.Status_DeviceStatus, Properties.Resources.None);

            TagStatusText.Text = string.Format(Properties.Resources.Status_TagStatus, Properties.Resources.None);

            ErrortatusText.Text = Properties.Resources.Status_NoErrorStatus;

        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        private void SetStatus(StatusData status)
        {
            switch (status.StatusType)
            {
                case StatusType.DeviceStatus:
                    DeviceStatusText.Text = string.Format(Properties.Resources.Status_DeviceStatus, status.Message);
                    break;
                case StatusType.TagStatus:
                    TagStatusText.Text = string.Format(Properties.Resources.Status_TagStatus, status.Message);
                    break;
                case StatusType.NoErrorStatus:
                    ErrortatusText.Text = Properties.Resources.Status_NoErrorStatus;
                    //Image
                    break;
                case StatusType.ErrorStatus:
                    ErrortatusText.Text = string.Format(Properties.Resources.Status_ErrorStatus, status.Message);
                    //Image
                    break;
                case StatusType.DeviceOperationStatus:
                    OperationStatusText.Text = string.Format(Properties.Resources.Status_LatestOperation, status.Message);
                    //Image
                    break;
                case StatusType.TagOperationStatus:
                    OperationStatusText.Text = string.Format(Properties.Resources.Status_LatestOperation, status.Message);
                    //Image
                    break;
            }
        }
    }
}
